/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vmwapplication.data;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 * pristup k dataum do db
 * @author marek
 */

public class DAO implements IDAO {
    
    private EntityManager em;

    public DAO(){
    }
    
    public DAO(EntityManager em){
        this.em = em;
    }
    
    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
        
    }
    
    
    public Test saveTest(Test test){
        if(!this.em.getTransaction().isActive()){
            this.em.getTransaction().begin();
        }
        em.persist(test);
        em.getTransaction().commit();
        
        return test;
    }
    
    public List<Test> findAll(){
        Query q = em.createQuery("select t from "+Test.class.getSimpleName()+" t ");
        return (List<Test>) q.getResultList();
    }
    
    
}
